package com.srcbox.file.ui

import android.graphics.BitmapFactory
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.fastjson.JSON
import com.gyf.immersionbar.ImmersionBar
import com.srcbox.file.R
import com.srcbox.file.adapter.DonationList
import com.srcbox.file.data.AppSetting
import com.srcbox.file.util.EggUtil
import com.srcbox.file.util.GlobalMethods
import com.srcbox.file.util.ProtocolDialog
import com.yang.easyhttp.EasyHttpClient
import com.yang.easyhttp.callback.EasyStringCallback
import kotlinx.android.synthetic.main.about.*
import java.io.File

class About : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.about)
        GlobalMethods(this).initActivityUi()
        initUi()
        initEvent()
    }

    private fun initEvent() {
        about_official_web_on.setOnClickListener {
            EggUtil.goWeb(this, AppSetting.APP_LINK)
        }
        about_protocol_on.setOnClickListener {
            val isProtoPath = "${filesDir.path}/isProto.txt"
            val isProto = File(isProtoPath)
            ProtocolDialog(this, isProto, true) {}.create()
        }
    }

    private fun initUi() {
        EggUtil.loadIcon(this, about_img, AppSetting.FONT_COLOR)
        /*GlobalMethods(this).initActivityUi()*/
        about_app__version_name.text =
            "App版本名：${packageManager.getPackageInfo(packageName, 0).versionName}"
        about_app__version_code.text =
            "App版本号：${packageManager.getPackageInfo(packageName, 0).versionCode}"

        about_egg_icon.setImageBitmap(BitmapFactory.decodeStream(assets.open("img/egg.png")))
        about_rate_icon.setImageBitmap(BitmapFactory.decodeStream(assets.open("img/rate.png")))

        meIdeaDonation.setOnClickListener {
            EggUtil.joinQQGroup(this, AppSetting.QUN_KEY)
            EggUtil.toast(this, "联系群主")
        }

        EasyHttpClient.get(
            "https://juyi-1253946182.cos.ap-chengdu.myqcloud.com/data/json/doantion_list.json",
            object : EasyStringCallback() {
                override fun onSuccess(content: String?) {
                    donationList.layoutManager = LinearLayoutManager(this@About)
                    donationList.adapter = DonationList(this@About, JSON.parseArray(content))
                }

                override fun onFailure(error: Throwable?, content: String?) {
                    EggUtil.toast(this@About, content!!)
                }
            })
    }
}

/* AlertDialog.Builder(this).setMessage(EggIO().readFile(assets.open("proto.txt"))).setTitle("协议")
          .setNegativeButton("确定") { _: DialogInterface, _: Int ->
          }.show()*/

/*
        pay_img.setImageBitmap(BitmapFactory.decodeStream(assets.open("img/pay.png")))
        egg_img.setImageBitmap(BitmapFactory.decodeStream(assets.open("img/egg.png")))
        rate_img.setImageBitmap(BitmapFactory.decodeStream(assets.open("img/rate.png")))*/

/* egg.setOnClickListener {
            EggUtil.goQQ(this, "1970284668")
        }

        wugui.setOnClickListener {
            EggUtil.goQQ(this, "3441152376")
        }


        pay.setOnClickListener {
            EggUtil.saveFile(
                assets.open("img/pay.png"),
                File("${AppSetting.fileStoragePath}pay.png")
            )
            try {
                EggUtil.startApp(this, "com.eg.android.AlipayGphone")
            } catch (e: Exception) {
                EggUtil.toast(this,"您的手机未安装支付宝")
            }
        }*/

/*
        val weixinImg = threePager.findViewById<ImageView>(R.id.weixin_img)
        val payImg = threePager.findViewById<ImageView>(R.id.pay_img)
        val eggImg = threePager.findViewById<ImageView>(R.id.egg_img)
        val rateImg = threePager.findViewById<ImageView>(R.id.rate_img)*/

/*       val eggCard = threePager.findViewById<CardView>(R.id.egg)
       val wuguiCard = threePager.findViewById<CardView>(R.id.wugui)
       val weixin = threePager.findViewById<LinearLayout>(R.id.weixin)
       val pay = threePager.findViewById<LinearLayout>(R.id.pay)*/